const path = require('path')

// 删除缓存
function requireUncached(module){
  delete require.cache[require.resolve(module)]
  return require(module)
}

function gulptasksJS($, gulp, buildFolder, browserSync){
  ///// Dev  

  // 监听js的变动，如果有修改就重新注入修改
  gulp.task('js.dev.watch', () => {
    return gulp
      .src('../src/js/main.js')
      .pipe(
        $.webpackStream(
          requireUncached('./webpack.config.js')({
            watch: true
          })
        )
      )
      .pipe(gulp.dest(buildFolder))
      // 这个的作用应该是给window做的，之前的代码在gulpfile.js的watch中监听不到js更新的
      // .pipe(browserSync.stream())
  })

  gulp.task('js.dev', () => {
    return gulp
      .src('../src/js/main.js')
      // 加载webpack配置文件并执行
      .pipe($.webpackStream(requireUncached('./webpack.config.js')({})))
      .pipe(gulp.dest(buildFolder))
  })

  ///// prod
  
  gulp.task('js.prod', () => {
    return gulp
      .src('../src/js/main.js')
      .pipe(
        $.webpackStream(
          requireUncached('./webpack.prod.config.js')({
            standalone: true,
            isApp: "false"
          })
        )
      )
      .pipe(gulp.dest(buildFolder))
  })
  
  gulp.task('js.prod.app', () => {
    return gulp
      .src('../src/js/main.js')
      .pipe(
        $.webpackStream(
          requireUncached('./webpack.prod.config.js')({
            standalone: true,
            isApp: 'true'
          })
        )
      )
      .pipe(gulp.dest(buildFolder))
  })
}

module.exports = {
  gulptasksJS
}